package com.jisf.driver.auth;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Classname JsonAuthentication
 * @Description TODO
 * @Date 2022/4/4 21:16
 * @Created by jisf：【429627912@qq.com】
 */
public abstract class JsonAuthentication {

    /**
     * 输出JSON
     * @param request
     * @param response
     * @param data
     * @throws IOException
     */
    protected void WriteJSON(HttpServletRequest request, HttpServletResponse response, Object data) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Method", "POST,GET");
        PrintWriter out = response.getWriter();
        out.write(new ObjectMapper().writeValueAsString(data));
        out.flush();
        out.close();
    }

}
